//商户审核
exports.sh = function(req , res){
	var mysql = require('mysql'),
	client = mysql.createConnection(myDb);
	client.connect();
	client.query("use " + myDb.DbName);	
	if(!req.param('mid')){
		if(req.param('s')=="1")
			var where = " `check` = 0 ";
		else if(req.param('s')=="2")
			var where = " `check` = 1 ";
		else
			var where = "1";
		client.query("SELECT * FROM  `360_merchant` WHERE "+where+" ORDER BY  `check` DESC ,  `addtime` DESC",
			function selectCb(err, returnData){
				console.log(returnData);
				if(returnData.length)
					res.render('merchant/index', { data:returnData});
				else
					res.render('merchant/index', { data:[]});
			}
		)
	}
	else{
		client.query("UPDATE `360_merchant` SET  `check` =  '"+req.param('type')*1+"' WHERE  `id` ="+req.param('mid')*1+" LIMIT 1 ;",
			function selectCb(err, returnData){
				if(returnData.length)
					res.send("1");
				else
					res.send("0");
			}
		)
	}
}



//商户发布
exports.add = function(req , res){
	var express = require('express'),
	formidable = require('formidable'),
	async = require('async'),
	md5 = require('md5'),
	fs = require('fs'),
	AVATAR_UPLOAD_FOLDER = '/merchant/',
	theTime = parseInt((new Date()).valueOf()/1000),
	mysql = require('mysql'),
	client = mysql.createConnection(myDb);
	client.connect();
	client.query("use " + myDb.DbName);	
	var theTime = parseInt((new Date()).valueOf()/1000);
	var form = new formidable.IncomingForm();
	form.encoding = 'utf-8';
	form.uploadDir = 'public' + AVATAR_UPLOAD_FOLDER;
	form.keepExtensions = true;
	//上传限制 3M;
	form.maxFieldsSize = 3 * 1024 * 1024;
	form.parse(req, function(err, fields, files){
		if(fields){
			var keycode = fields['key'].replace(' ','+');
			var keycode = aes.decode(keycode , key);
			var keycode = keycode.split('|');
			var merchantName = fields.merchantName ? fields.merchantName : "";
			var annotate = fields.annotate ? fields.annotate : "";
			var coordinate = fields.coordinate ? fields.coordinate : "";
			var uid = keycode[2];
			var nickName = keycode[3];
				if(uid){
					async.waterfall([
						function(callback){
							if(files.file.size){
								var extName = '';  //后缀名
								switch (files.file.type) {
									case 'image/pjpeg':
									extName = 'jpg';
									break;
									case 'image/jpeg':
									extName = 'jpg';
									break;
									case 'image/gif':
									extName = 'gif';
									break;						
									case 'image/png':
									extName = 'png';
									break;
									case 'image/x-png':
									extName = 'png';
									break;	 
								}
								if(extName.length > 0){
									var avatarName = '' + md5(Math.random()).substr(0,14) + '.' + extName;
									var newPath = form.uploadDir + avatarName;
									//重命名
									fs.renameSync(files.file.path, newPath);
									addData = {uid:uid,uName:nickName,merchantName:merchantName,annotate:annotate,coordinate:coordinate,path:AVATAR_UPLOAD_FOLDER+avatarName};
								}
								else
									//文件格式错误;
									var path1State = true;
							}
							else
								addData = {uid:uid,uName:nickName,merchantName:merchantName,annotate:annotate,coordinate:coordinate,path:""};
							if(files.file1.size){
								var extName = '';  //后缀名
								switch (files.file1.type) {
									case 'image/pjpeg':
									extName = 'jpg';
									break;
									case 'image/jpeg':
									extName = 'jpg';
									break;
									case 'image/gif':
									extName = 'gif';
									break;						
									case 'image/png':
									extName = 'png';
									break;
									case 'image/x-png':
									extName = 'png';
									break;	 
								}
								if(extName.length > 0){
									var avatarName = '' + md5(Math.random()).substr(0,14) + '.' + extName;
									var newPath = form.uploadDir + avatarName;
									//重命名
									fs.renameSync(files.file1.path, newPath);
									addData.path1 = AVATAR_UPLOAD_FOLDER+avatarName;
								}
								else
									//文件格式错误;
									var path2State = true;
							}
							else
								addData.path1 = "";
							
							if(path1State && path2State)
								res.send(JSON.stringify({code:'2'}));
							
							callback(null,addData);
						},
						function(a1 , callback){
							client.query("INSERT INTO `360_merchant` (`id`, `uid`, `merchantName`, `picturePath1`, `picturePath2`, `annotate`, `coordinate`, `check`, `addtime`) VALUES (NULL, '"+a1.uid+"', '"+a1.merchantName+"', '"+a1.path+"', '"+a1.path1+"', '"+a1.annotate+"', '"+a1.coordinate+"', 0, '"+theTime+"');",
								function selectCb(err, returnData){
									if(returnData.insertId)
										res.send(JSON.stringify({code:'1'}));
									else
										res.send(JSON.stringify({code:'3'}));
								}
							)
						},
						function(b1 , callback){
							
						}],
						function (err, result) {
							return;
						}
					)
				}
				else{
					
				}
		}
		else
			res.send(JSON.stringify({code:'2'}));
	})
}





//商户编辑
exports.edit = function(req , res){
	var express = require('express'),
	formidable = require('formidable'),
	async = require('async'),
	md5 = require('md5'),
	fs = require('fs'),
	AVATAR_UPLOAD_FOLDER = '/merchant/',
	theTime = parseInt((new Date()).valueOf()/1000),
	mysql = require('mysql'),
	client = mysql.createConnection(myDb);
	client.connect();
	client.query("use " + myDb.DbName);	
	var theTime = parseInt((new Date()).valueOf()/1000);
	var form = new formidable.IncomingForm();
	form.encoding = 'utf-8';
	form.uploadDir = 'public' + AVATAR_UPLOAD_FOLDER;
	form.keepExtensions = true;
	//上传限制 3M;
	form.maxFieldsSize = 3 * 1024 * 1024;
	form.parse(req, function(err, fields, files){
		if(fields){
			var keycode = fields['key'].replace(' ','+');
			var keycode = aes.decode(keycode , key);
			var keycode = keycode.split('|');
			var merchantName = fields.merchantName ? fields.merchantName : "";
			var annotate = fields.annotate ? fields.annotate : "";
			var coordinate = fields.coordinate ? fields.coordinate : "";
			var mid = fields.mid?fields.mid*1:false;
			var uid = keycode[2];
			var nickName = keycode[3];
				if(uid && mid){
					async.waterfall([
						function(callback){
							if(files.file.size){
								var extName = '';  //后缀名
								switch (files.file.type) {
									case 'image/pjpeg':
									extName = 'jpg';
									break;
									case 'image/jpeg':
									extName = 'jpg';
									break;
									case 'image/gif':
									extName = 'gif';
									break;						
									case 'image/png':
									extName = 'png';
									break;
									case 'image/x-png':
									extName = 'png';
									break;	 
								}
								if(extName.length > 0){
									var avatarName = '' + md5(Math.random()).substr(0,14) + '.' + extName;
									var newPath = form.uploadDir + avatarName;
									//重命名
									fs.renameSync(files.file.path, newPath);
									addData = {uid:uid,uName:nickName,merchantName:merchantName,annotate:annotate,coordinate:coordinate,path:'"'+AVATAR_UPLOAD_FOLDER+avatarName+'"'};
								}
								else
									//文件格式错误;
									addData = {uid:uid,uName:nickName,merchantName:merchantName,annotate:annotate,coordinate:coordinate,path:'`picturePath1`'};
							}
							else
								addData = {uid:uid,uName:nickName,merchantName:merchantName,annotate:annotate,coordinate:coordinate,path:'`picturePath1`'};
							if(files.file1.size){
								var extName = '';  //后缀名
								switch (files.file1.type) {
									case 'image/pjpeg':
									extName = 'jpg';
									break;
									case 'image/jpeg':
									extName = 'jpg';
									break;
									case 'image/gif':
									extName = 'gif';
									break;						
									case 'image/png':
									extName = 'png';
									break;
									case 'image/x-png':
									extName = 'png';
									break;	 
								}
								if(extName.length > 0){
									var avatarName = '' + md5(Math.random()).substr(0,14) + '.' + extName;
									var newPath = form.uploadDir + avatarName;
									//重命名
									fs.renameSync(files.file1.path, newPath);
									addData.path1 = '"'+AVATAR_UPLOAD_FOLDER+avatarName+'"';
								}
								else
									//文件格式错误;
									addData.path1 = "`picturePath2`";
							}
							else
								addData.path1 = "`picturePath2`";
							callback(null,addData);
						},
						function(a1 , callback){
							client.query("UPDATE `360_merchant` SET  `merchantName` =  '"+a1.merchantName+"',`picturePath1` =  "+a1.path+",`picturePath2` =  "+a1.path1+",`annotate` =  '"+a1.annotate+"',`coordinate` =  '"+a1.coordinate+"' WHERE  `id` ="+mid+" LIMIT 1 ;",
								function selectCb(err, returnData){
									console.log(returnData);
									if(returnData)
										res.send(JSON.stringify({code:'1'}));
									else
										res.send(JSON.stringify({code:'3'}));
								}
							)
						},
						function(b1 , callback){
							
						}],
						function (err, result) {
							return;
						}
					)
				}
				else
					res.send(JSON.stringify({code:'3'}));
		}
		else
			res.send(JSON.stringify({code:'2'}));
	})
}





//商户列表
exports.list = function(req , res){
	var list = req.body.list ? req.body.list*1 : 1;
	var theTime = parseInt((new Date()).valueOf()/1000);
	var mysql = require('mysql');
	var list = req.body.list?req.body.list*1:1;
	var pageStart = list*5-5;
	var pageEnd = list*5;
	var client = mysql.createConnection(myDb);
		client.connect();
		client.query("use " + myDb.DbName);
		console.log("SELECT a.id as mid,a.merchantName,a.picturePath1,a.picturePath2,a.annotate,a.coordinate,a.addtime,b.id as uid,b.nickName,b.headPath FROM  `360_merchant` as a join 360_member as b where a.check = 1 and a.uid = b.id and a.id>"+pageStart+ " and a.id<="+pageEnd);
		client.query("SELECT a.id as mid,a.merchantName,a.picturePath1,a.picturePath2,a.annotate,a.coordinate,a.addtime,b.id as uid,b.nickName,b.headPath FROM  `360_merchant` as a join 360_member as b where a.check = 1 and a.uid = b.id and a.id>"+pageStart+ " and a.id<="+pageEnd,
			function selectCb(err, returnData){
				if(returnData.length){
					var row = {};
					row.code = "1";
					row.data = returnData;
					res.send(JSON.stringify(row));
				}
				else
					res.send(JSON.stringify({code:'1',data:[]}));
			}
		)
}


//商户查看
exports.view = function(req , res){
	var mysql = require('mysql');
	var mid = req.body.mid*1;
	if(mid){
		var client = mysql.createConnection(myDb);
			client.connect();
			client.query("use " + myDb.DbName);
			console.log("SELECT a.id as mid,a.merchantName,a.picturePath1,a.picturePath2,a.annotate,a.coordinate,a.addtime,b.id as uid,b.nickName,b.headPath FROM  `360_merchant` as a join 360_member as b where a.check = 0 and a.uid = b.id and a.id="+mid);
			client.query("SELECT a.id as mid,a.merchantName,a.picturePath1,a.picturePath2,a.annotate,a.coordinate,a.addtime,b.id as uid,b.nickName,b.headPath FROM  `360_merchant` as a join 360_member as b where a.check = 0 and a.uid = b.id and a.id="+mid,
				function selectCb(err, returnData){
					if(returnData.length){
						var row = {};
						row.code = "1";
						row.data = returnData;
						res.send(JSON.stringify(row));
					}
					else
						res.send(JSON.stringify({code:'1',data:[]}));
				}
			)
	}
	else
		res.send(JSON.stringify({code:'2'}));
	
}